package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.StringUtils;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysUserSociety;
import com.ruoyi.system.mapper.SocietyManageMapper;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysFinanceLogMapper;
import com.ruoyi.system.domain.SysFinanceLog;
import com.ruoyi.system.service.ISysFinanceLogService;

/**
 * 财务资金Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-28
 */
@Service
public class SysFinanceLogServiceImpl implements ISysFinanceLogService {

  @Autowired
  private SysFinanceLogMapper sysFinanceLogMapper;
  @Autowired
  private SocietyManageMapper societyManageMapper;

  /**
   * 查询财务资金
   *
   * @param financeId 财务资金主键
   * @return 财务资金
   */
  @Override
  public SysFinanceLog selectSysFinanceLogByFinanceId(Long financeId) {
    return sysFinanceLogMapper.selectSysFinanceLogByFinanceId(financeId);
  }

  /**
   * 查询财务资金列表
   *
   * @param sysFinanceLog 财务资金
   * @return 财务资金
   */
  @Override
  public List<SysFinanceLog> selectSysFinanceLogList(SysFinanceLog sysFinanceLog) {
    String roleName = SecurityUtils.getRoleName();
    Long userId = SecurityUtils.getUserId();
    String societyIdJoin = null;
    if ("学生".equals(roleName)) {
      List<Long> integers = societyManageMapper.querySheQu(userId);
      societyIdJoin = integers.stream()
          .map(String::valueOf).collect(Collectors.joining(","));
      if (StringUtils.isEmpty(societyIdJoin)) {
        societyIdJoin = " ";
      }
    }
    List<String> societyNameList = societyManageMapper.querySocietyNameList(
        userId);


    if (!SecurityUtils.isTeacher()) {
      String join = String.join(",", societyNameList);
      sysFinanceLog.setJoin(join);
    }

    sysFinanceLog.setSocietyNameList(societyNameList);
    sysFinanceLog.setSocietyIdJoin(societyIdJoin);

    return sysFinanceLogMapper.selectSysFinanceLogList(sysFinanceLog);
  }

  /**
   * 新增财务资金
   *
   * @param sysFinanceLog 财务资金
   * @return 结果
   */
  @Override
  public int insertSysFinanceLog(SysFinanceLog sysFinanceLog) {
    return sysFinanceLogMapper.insertSysFinanceLog(sysFinanceLog);
  }

  /**
   * 修改财务资金
   *
   * @param sysFinanceLog 财务资金
   * @return 结果
   */
  @Override
  public int updateSysFinanceLog(SysFinanceLog sysFinanceLog) {
    return sysFinanceLogMapper.updateSysFinanceLog(sysFinanceLog);
  }

  /**
   * 批量删除财务资金
   *
   * @param financeIds 需要删除的财务资金主键
   * @return 结果
   */
  @Override
  public int deleteSysFinanceLogByFinanceIds(Long[] financeIds) {
    return sysFinanceLogMapper.deleteSysFinanceLogByFinanceIds(financeIds);
  }

  /**
   * 删除财务资金信息
   *
   * @param financeId 财务资金主键
   * @return 结果
   */
  @Override
  public int deleteSysFinanceLogByFinanceId(Long financeId) {
    return sysFinanceLogMapper.deleteSysFinanceLogByFinanceId(financeId);
  }
}
